package com.dhy.takeaway.controller;

import com.dhy.takeaway.entity.Res;
import com.dhy.takeaway.entity.Store;
import com.dhy.takeaway.entity.User;
import com.dhy.takeaway.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class LoginController {

    @Autowired
    LoginService loginService;

    @PostMapping("/login")
    public Res login(@RequestBody Map<String,String> form){
        System.out.println("用户id和密码：" + form);
        Res res = new Res();
        String username = form.get("username");
        String password = form.get("password");
        if(username==null || username==""){
            res.setCode("400");
            res.setMessage("username为空");
        }else if(password==null || password==""){
            res.setCode("400");
            res.setMessage("password为空");
        }

        User user = loginService.loginSuccess(username);
        if(user!=null){
            if (user.getPassword().equals(password)) {
                res.setCode("200");
                res.setMessage("登录验证成功！");
                res.setObj(user.getId());
            } else {
                res.setCode("400");
                res.setMessage("登录验证失败！");
            }
        }

        return res;
    }

    @PostMapping("/isExist")
    public boolean userNameIsExist(@RequestBody Map<String,String> map){
        String username = map.get("username");
        if(username==null || username==""){
            return false;
        }else if(loginService.userNameIsExist(username)){
            return false;
        }else {
            return true;
        }
    }

    @PostMapping("/register")
    public Res register(@RequestBody Map<String,String> form){
        System.out.println("注册用户id和密码：" + form);
        Res res = new Res();
        String username = form.get("username");
        String password = form.get("password");
        if(username==null || username==""){
            res.setCode("400");
            res.setMessage("username为空");
        }else if(password==null || password==""){
            res.setCode("400");
            res.setMessage("password为空");
        }

        else if(loginService.register(new User(username,password))){
            res.setCode("200");
        }else{
            res.setCode("400");
            res.setMessage("用户名或密码错误！");
        }

        return res;
    }

    @PostMapping("/checkPass")
    public Res checkPass(@RequestBody User user){
        Res res = new Res();
        if(loginService.checkPass(user)){
            res.setCode("200");
            res.setMessage("密码正确！");
        } else {
            res.setCode("400");
            res.setMessage("密码错误!");
        }
        return res;
    }

    @PostMapping("/modifyPass")
    public Res modifyPass (@RequestBody Map<String, String> map){
        Res res = new Res();
        if (loginService.changePass(map)) {
            res.setCode("200");
            res.setMessage("密码修改成功！");
        } else {
            res.setCode("400");
            res.setMessage("密码修改失败！");
        }
        return res;
    }

    @PostMapping("/changeStore")
    public Res changeStore(@RequestBody Store store){
        Res res = new Res();
        if (loginService.updateStore(store)) {
            res.setCode("200");
            res.setMessage("店面修改成功！");
        } else {
            res.setCode("400");
            res.setMessage("店面修改失败！");
        }

        return res;
    }

    @PostMapping("/findStore")
    public Store findStore(@RequestBody Map<String,String> map){
        return loginService.findStore(map.get("userId"));
    }
}
